package com.authine.cloudpivot.web.api.service.impl;


import com.authine.cloudpivot.web.api.dao.*;
import com.authine.cloudpivot.web.api.domain.Constants;
import com.authine.cloudpivot.web.api.service.ResourcesDistributionService;
import com.authine.cloudpivot.web.api.vo.ResourcesDistribution;
import com.authine.cloudpivot.web.api.vo.TimingTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ResourcesDistributionServiceImpl implements ResourcesDistributionService {



    @Autowired
    private ResourcesDistributionMapper resourcesDistributionMapper;

    @Autowired
    private TimingTaskServiceImpl  timingTaskServiceImpl;


        /**
         * @Description : 任务分发：招聘或店检，提醒执行经理
         * @param ： projectNames
         * @param ： type
         * @param ： status
         * @param ： typeIds
         * @author : Liun
         * @date : 2020/4/24
         */
        @Override
        public void remindEManager(String objectId,String projectNames,String takeType,String task,String distribute){

            SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String nowTime=sf.format(new Date());

            List<String>  persons=new ArrayList<>();
            //招聘
            if(Constants.APERIODIC.equals(takeType)&&Constants.RECRUIT.equals(task)&&Constants.YES.equals(distribute)){
              List<String>  recruitPersons=resourcesDistributionMapper.selectPersonForRecruit(objectId,projectNames,nowTime);
              persons.addAll(recruitPersons);
            }
            //店检
            if(Constants.APERIODIC.equals(takeType)&&Constants.SHOP_INSPECTION.equals(task)&&Constants.YES.equals(distribute)){
               List<String>  shopPersons=resourcesDistributionMapper.selectPersonForShop(objectId,projectNames,nowTime);
               persons.addAll(shopPersons);
            }

            //及时给发送招聘、店检关联的门店督导发送通知
            if(persons.size()>0){
                String userIDS="";
                String userNames="";
                for(String person:persons){
                    if(StringUtils.isEmpty(userIDS)){
                        userIDS += timingTaskServiceImpl.createReceivers(person);
                        userNames+=timingTaskServiceImpl.getPersonNames(person);
                    }else{
                        userIDS += ","+timingTaskServiceImpl.createReceivers(person);
                        userNames+=","+timingTaskServiceImpl.getPersonNames(person);
                    }
                }
                //回写分发结果
                boolean result = timingTaskServiceImpl.sendMessage(userIDS);
                if(result){
                    updateDistributeResult(objectId,userNames);
                }
            }
        };

    /**
     * @Description : 根据当前人员(执行经理)，当前项目，获取大区
     * @param ： userID
     * @param ： projectNames
     * @author : Liun
     * @date : 2020/4/24
     */
    @Override
    public String findAreaByEManager(String projectNames,String userID){
        String area=resourcesDistributionMapper.findAreaByEManager(projectNames,userID);
        return area;
     };


    /**
     * @Description : 修改任务分发中的分发结果
     * @param ：
     * @author : Liun
     * @date : 2020/4/24
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDistributeResult(String  rdID,String names){
        ResourcesDistribution rd=resourcesDistributionMapper.selectByPrimaryKey(rdID);
        rd.setDistributeresult(names);
        resourcesDistributionMapper.updateByPrimaryKey(rd);
    }



    /**
     * @Description : 根据任务分发id，查找到对应的任务列表数据
     * @param ： objectId
     * @author : Liun
     * @date : 2020/4/24
     */
    public List<TimingTask> selectUserByRds(String objectId){
       List<TimingTask> persons=resourcesDistributionMapper.selectUserByRds(objectId);
       return persons;
    }



};











